package com.kystar.muxplayer.ui;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kystar.muxplayer.R;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class SubGroupView implements Collapse {
    final View root;
    final View divider;
    final View title;
    final ViewGroup content;
    final ImageView arrow;
    final boolean last;

    public SubGroupView(Context context, String name, int contentId, boolean last) {
        this.last = last;

        root = View.inflate(context, R.layout.layout_main_settings_group_item, null);
        ((TextView)root.findViewById(R.id.settings_group_item_name)).setText(name);
        title = root.findViewById(R.id.settings_group_item_title);

        content = root.findViewById(R.id.settings_group_item_content);
        content.addView(View.inflate(context, contentId, null),
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        arrow = root.findViewById(R.id.settings_group_item_title_arrow);
        divider = root.findViewById(R.id.settings_group_item_divider);
        if (last) {
            divider.setVisibility(View.GONE);
        }
    }

    @Override
    public void collapse(boolean toggle) {
        if (toggle && content.getVisibility() == View.GONE) {
            content.setVisibility(View.VISIBLE);
            arrow.setImageResource(R.drawable.settings_subtitle_up);
            if (divider != null && !last) {
                divider.setVisibility(View.GONE);
            }
        } else {
            content.setVisibility(View.GONE);
            arrow.setImageResource(R.drawable.settings_subtitle_down);
            if (divider != null && !last) {
                divider.setVisibility(View.VISIBLE);
            }
        }
    }
}
